$buff_min = 2400; #40 minutes
$buff_max = 4800; #80 minutes
$countdown = 5; # Countdown time to MGB in minutes, default 5 minutes
$announce_min = 1200; #20 minutes
$announce_max = 2400; #40 minutes


@SpellList = ("Clarity", "Boon of the Clear Mind", "Gift of Insight", "Koadic's Endless Intellect", "Voice of Clairvoyance", "Quickness", "Celerity", "Augment", "Vallon's Quickening", "Hastening of Salik", "Rune of Rikkukin");
@SpellCost = ("5",       "15",                     "15",              "60",                         "100",                   "5",         "10",       "15",      "30",                  "60",                 "30");
@SpellID = ("174",       "1694",                   "1409",            "2570",                       "5522",                  "39",        "171",      "1729",    "3178",                "5521",               "6671");

sub EVENT_SPAWN
{
   MASS_GROUP_BUFF();
   ANNOUNCE();
}

sub ANNOUNCE
{
   $announce_timer = int(rand($announce_max)) + $announce_min;
   quest::settimer("announce_darwig", $announce_timer); # Random announcements
}

sub MASS_GROUP_BUFF
{
   $mgb_timer = int(rand($buff_max)) + $buff_min;
   quest::settimer("mgb_darwig", $mgb_timer);
   $minutes = $countdown;
}

sub EVENT_TIMER
{
   if($timer eq "mgb_darwig")
      {
      quest::shout("Mass group buff across from Senara by the Big Bank, come one, come all!!");
      quest::stoptimer("mgb_darwig");
      quest::stoptimer("announce_darwig");
      quest::settimer("countdown_darwig", 60); # 60 second count before each countdown announcement
      }
     
   if($timer eq "countdown_darwig")
      {
      if($minutes > 0)
         {
         quest::shout("Casting Koadic's Endless Intellect MGB in " . $minutes . " minutes.");
         $minutes--;
         }
      else
         {
         quest::shout("Casting Koadic's Endless Intellect now !");
         quest::stoptimer("countdown_darwig");
         $npc->SetAppearance(0);
         my @moblist = $entity_list->GetMobList();
         foreach $ent (@moblist)
         {
            my $dist = $ent->CalculateDistance($npc->GetSpawnPointX(),$npc->GetSpawnPointY(),$npc->GetSpawnPointZ());
         plugin::DoAnim(cast);
            if(int($dist)<100 && $ent->IsClient())
            {
            $ent->SpellFinished(2570, $ent, 10);
            }
         }
         ANNOUNCE();
         MASS_GROUP_BUFF();
         }
      }

   if($timer eq "announce_darwig")
      {
      my $random_number = int(rand(7));
      if ($random_number == 0)
         {
         quest::shout("Big Mind and Body Buffs for small Money! See me behind the main bank.");
         }
      elsif ($random_number == 1)
         {
         quest::shout("Need Mind Candy? Come see me across from Senara by the main bank.");
         }
      elsif ($random_number == 2)
         {
         quest::shout("Can't beat your enemys fast enough? I will change that! Behind the main bank.");
         }
      elsif ($random_number == 3)
         {
         quest::shout("Buffing for donations beside the main bank.");
         }
      elsif ($random_number == 4)
         {
         quest::shout("Mind and Body Buffs will make your life easier. See me behind the main bank.");
         }
      elsif ($random_number == 5)
         {
         quest::shout("Blessings? With my buffs you don't need that! See me behind the main bank.");
         }
      elsif ($random_number == 6)
         {
         quest::shout("My Crack is the best of all Norrath! It will blast .. anyway, see me behind the main bank.");
         }
      $npc->SetAppearance(int(rand(2)));
      ANNOUNCE();
      }
     
}

sub EVENT_SAY {

   if (!$cpl) { # We create the List of Spells just once per zone load > npc hail.
      $cpl = 1;
      my $n = 0;
      @buffs = ();
      while ($SpellList[$n]) {
         my $SpellList = quest::saylink($n, 1, $SpellList[$n]);
         my $SpellCost = $SpellCost[$n];
         push (@buffs, "$SpellList for $SpellCost PP");   
         $n++;
      }
      $buff = join(", ",@buffs);
   }
   
   
   my $NPCName = $npc->GetCleanName();
   $client->Message(315, " ");
   if ($text=~/hail/i) {
      my $sl1 = quest::saylink("buff", 1);
      $client->Message(315, "$NPCName says, 'Greetings, $name. In need of Mind and Body hasting ? For a small donation i will $sl1 you.'");
      plugin::DoAnim(salute);
   } elsif ($text=~/buff/i) {
      $client->Message(315, "$NPCName says, 'Great! Which are you interested in?'");
      $client->Message(12, "Warning! By clicking on the name of a Buff, you will be charged automatically for the asked amount.");
        $client->Message(315, "$buff");
   } else {
      if (($text =~ m/\d/) && $SpellList[$text]) {
         my $PCMoney = $client->GetCarriedMoney();
         my $Cost = $SpellCost[$text]*1000;
         if ($PCMoney >= $Cost) {
            $client->Message(315, "$NPCName says, 'Thank you $name! I will now cast $SpellList[$text] for you. Please come again!");
            plugin::DoAnim(cast);
            quest::selfcast($SpellID[$text]);
            $client->TakeMoneyFromPP($Cost, true);
         } else {
            $client->Message(315, "$NPCName says, 'I am sorry $name, you don't have enough Money with you.");
         }
      }
   }
}

sub EVENT_ITEM {
   my $NPCName = $npc->GetCleanName();
   $client->Message(315, "$NPCName says, 'Thank you, but you should hold on to it.'");
   plugin::return_items(\%itemcount);
   if ($platinum != 0 || $gold !=0 || $silver != 0 || $copper != 0) {
      quest::givecash($copper, $silver, $gold, $platinum);
   }
}